/*
 * TryTagsFilter.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package trypkg
import java.io._
import java.util.regex._
object TryTagsFilter {
    def main(args : Array[String]) : Unit ={
        val strUrl = "http://club.stock.sohu.com/r-stock-10701074-0-0-900.html"
        val in = new InputStreamReader(new java.net.URL(strUrl).openStream)
        var input = new java.lang.StringBuilder
        var c = in.read
        while(c != -1){
            input.append(c.asInstanceOf[Char])
            //println("c= " + c)
            c = in.read
        }

        input = killStyle(input)
        input = killScript(input)

        println(input)
    }

    def killStyle(oldInput : java.lang.StringBuilder) : java.lang.StringBuilder = {
        var input = oldInput.toString
        if(input != null){
            var StyleStart : Int = input.indexOf("<style")
            println("StyleStart = " + StyleStart)
            var StyleEnd : Int = input.indexOf("</style>")
            println("StyleEnd = " + StyleEnd)
            try{
                while(input.contains("<style") && StyleStart != -1){
                   
                    if(StyleStart != -1 && StyleEnd != -1){
                        var strStart = input.subSequence(0, StyleStart)
                        var strEnd = input.substring(StyleEnd, input.length)
                        input = strStart + strEnd
                    }
                    StyleStart = input.indexOf("<style")
                    println("StyleStart = " + StyleStart)
                    StyleEnd = input.indexOf("</style>")
                    println("StyleEnd = " + StyleEnd)
                }
            }catch{
                case e : Exception =>{
                        println(e.toString)
                    }
            }

            var UpcaseStyleStart : Int = input.indexOf("<STYLE")
            println("UpcaseStyleStart = " + UpcaseStyleStart)
            var UpcaseStyleEnd : Int = input.indexOf("</STYLE>")
            println("UpcaseStyleEnd = " + UpcaseStyleEnd)
            try{
                while(input.contains("<STYLE") && UpcaseStyleStart != -1){
                    if(UpcaseStyleStart != -1 && UpcaseStyleEnd != -1){
                        var a = input.substring(0, UpcaseStyleStart)
                        var b = input.substring(UpcaseStyleEnd, input.length)

                        input = a + b
                    }
                    UpcaseStyleStart = input.indexOf("<STYLE")
                    println("UpcaseStyleStart = " + UpcaseStyleStart)
                    UpcaseStyleEnd = input.indexOf("</STYLE>")
                    println("UpcaseStyleEnd = " + UpcaseStyleEnd)
                }
            }catch{
                case e : Exception =>{
                        println(e.toString)
                    }
            }
        }
        input.asInstanceOf[java.lang.StringBuilder]
    }

    /**
     filter the script in the page
     @param  input : java.lang.StringBuilder
     @return java.lang.StringBuilder
     */
    def killScript(oldInput : java.lang.StringBuilder) : java.lang.StringBuilder = {
        var input = oldInput.toString
        if(input != null){
            try{
                var ScriptStart : Int = input.indexOf("<script")
                println("ScriptStart = " +ScriptStart)
                var ScriptEnd : Int = input.indexOf("</script>")
                println("ScriptEnd = " + ScriptEnd)

                while(input.contains("<script") && ScriptStart != -1){
                    try{
                        if(ScriptStart != -1 && ScriptEnd != -1){
                            var a = input.substring(0, ScriptStart)
                            var b = input.subSequence(ScriptEnd, input.length)
                            input = a + b
                        }
                        ScriptStart = input.indexOf("<script")
                        println("ScriptStart = " +ScriptStart)
                        ScriptEnd = input.indexOf("</script>")
                        println("ScriptEnd = " + ScriptEnd)
                        println(input.length)
                    }catch{
                        case e : Exception =>{
                                ScriptStart = -1
                                ScriptEnd = -1
                                println(e.toString)
                            }
                    }
                }

                var UpcaseScriptStart : Int = 0
                println("ScriptStart = " +UpcaseScriptStart)
                var UpcaseScriptEnd : Int = 0
                println("ScriptEnd = " + UpcaseScriptEnd)

                while(input.toString.contains("<script")){
                    try{
                        UpcaseScriptStart = input.indexOf("<script")
                        println("ScriptStart = " + UpcaseScriptStart)
                        UpcaseScriptEnd = input.indexOf("</script>")
                        println("ScriptEnd = " + UpcaseScriptEnd)

                        if(UpcaseScriptStart != -1 && UpcaseScriptEnd != -1){
                            var a = input.substring(0, UpcaseScriptStart)
                            var b = input.substring(UpcaseScriptEnd, input.length)
                            input = a + b
                        }
                        println(input.length)
                    }catch{
                        case e : Exception =>{
                                UpcaseScriptStart = -1
                                UpcaseScriptEnd = -1
                                println(e.toString)
                            }
                    }
                }
            }catch{
                case e : Exception =>{
                        println(e.toString)
                    }
            }
        }
        input.asInstanceOf[java.lang.StringBuilder]
    }
}
